package com.thb.user.service.Impl;

import java.util.HashMap;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.thb.exception.ExceptionCodeUtil;
import com.thb.exception.ServiceException;
import com.thb.logs.utils.LogUtils;
import com.thb.user.dao.IUserCodeDAO;
import com.thb.user.model.UserCode;
import com.thb.user.service.IUserCodeService;
import com.thb.utils.RandomUtils;

@Service
public class UserCodeServiceImpl implements IUserCodeService {
	
	private static Logger logger = LogUtils.getUserLog();
	
	public static final HashMap<String, String> rMap = new HashMap<String, String>();
	public static final HashMap<String, UserCode> sMap = new HashMap<String, UserCode>();
	
	@Autowired
	private IUserCodeDAO usercodeDao;

	@Override
	public boolean add(String passwd, byte type, String usercode) {
		logger.info("add user usercode:{}",usercode);
		
		UserCode user = findInfoByUsercode(usercode);
		if(user != null){
			throw new ServiceException("1000", "用户已存在");
		}
		
		user = new UserCode();
		user.setPasswd(passwd);
		user.setType(type);
		user.setUsercode(usercode);
		insert(user);
		return true;
	}

	@Override
	public UserCode findInfoByUsercode(String usercode) {
		UserCode user = usercodeDao.findByUsercode(usercode);
		return user;
	}

	@Override
	public UserCode findInfoByUid(int uid) {
		UserCode user = usercodeDao.findByUid(uid);
		return user;
	}

	@Override
	public boolean updatePasswd(int uid, String passwd) {
		usercodeDao.updatePwd(uid, passwd);
		return true;
	}
	
	private int insert(UserCode user){
		 int uid = usercodeDao.insert(user);
		 return uid;
	}
	
	@Override
	public String getRandom(String usercode){
		String ran = RandomUtils.getRandom(6);
		rMap.put(usercode, ran);
		return ran;
	}
	
	private String getSidRandom(){
		String ran = RandomUtils.getRandom(10);
		return ran;
	}
	
	@Override
	public boolean checkRandom(String usercode,String ran){
		String r = rMap.get(usercode);
		if(r == null){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "验证码不正确");
		}
		if(r.equals(ran)){
			rMap.remove(usercode);
			return true;
		}
		throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "验证码不正确");
	}

	@Override
	public String login(String usercode, String passwd, byte utyper) {
		UserCode user = findInfoByUsercode(usercode);
		if(user == null || !(user.getPasswd().equals(passwd))){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "用户名或者密码错误");
		}
		String sid = getSid(user);
		return sid;
	}
	
	private String getSid(UserCode user){
		long time = System.currentTimeMillis();
		String ran = getSidRandom();
		String sid = String.format("%s.%s.%s.%s", ran,user.getUid(),user.getUsercode(),time);
		sMap.put(sid, user);
		return sid;
	}

	@Override
	public UserCode getBySid(String sid) {
		UserCode user = sMap.get(sid);
		return user;
	}
	
	

	/*@Override
	public boolean add(String uid, String passwd, byte type, String usercode) {
		
		UserCode u = findInfoByUsercode(usercode);
		if(u != null){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "用户已存在");
		}
		
		UserCode user = new UserCode();
		user.setPasswd(passwd);
		user.setType(type);
		user.setUsercode(usercode);
		user.setUserId(uid);
		usercodeDao.insert(user);
		MemcachedUtil.remove(MKeyUtils.getUserCodeKey(usercode));
		return true;
	}

	@Override
	public UserCode findInfoByUsercode(String usercode) {
		UserCode user = null;
		Object o = MemcachedUtil.get(MKeyUtils.getUserCodeKey(usercode));
		if(o != null && !(o instanceof NullObject)){
			return (UserCode)o;
		}else if( o == null){
			user = usercodeDao.findByUsercode(usercode);
			MemcachedUtil.set(MKeyUtils.getUserCodeKey(usercode), user != null?user:NullObject.nullObject);
		}
		
		return user;
	}


	@Override
	public boolean updateUserCode(String uid, String usercode) {
		usercodeDao.updateUserCode(uid, usercode, UserConstant.utype_email);
		MemcachedUtil.remove(MKeyUtils.getUserCodeKey(usercode));
		return true;
	}

	@Override
	public boolean updateUserNick(String uid, String nick) {
		UserCode user = findInfoByUsercode(nick);
		if(user != null && uid != user.getUserId()){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "用户不存在");
		}
		usercodeDao.updateUserCode(uid, nick, UserConstant.utype_nick);
		MemcachedUtil.remove(MKeyUtils.getUserCodeKey(nick));
		return true;
	}

	@Override
	public boolean updatePasswd(String uid, String passwd, String usercode,
			String userNick) {
		usercodeDao.updatePwd(uid, passwd);
		MemcachedUtil.remove(MKeyUtils.getUserCodeKey(userNick));
		MemcachedUtil.remove(MKeyUtils.getUserCodeKey(usercode));
		return true;
	}

	@Override
	public synchronized String getNick() {
		long count = MemcachedUtil.incr(MKeyUtils.getUserCountKey(), 1l);
		if(count ==1){
			int ids = usercodeDao.findMaxId();
			if(ids > count){
				count = MemcachedUtil.incr(MKeyUtils.getUserCountKey(), ids);
			}
		}
		logger.info(String.format(" getNick userCount [ %s ]", count));
		String rString = String.format("%s%03d", UserConstant.nick,count);
		logger.info(String.format(" getNick result [ %s ]", rString));
		return rString;
	}*/
	
}
